/**
 * Project Name:SM Maven Webapp
 * File Name:ControllerLogServiceImpl.java
 * Package Name:cn.sh.ideal.mir.session.service.impl
 * Date:2015年2月5日下午4:38:38
 * Copyright (c) 2015, 上海理想信息产业（集团） All Rights Reserved.
 *
 */

package cn.sh.ideal.log.controller.service.impl;


import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sh.ideal.log.controller.LogControllerSqlSession;
import cn.sh.ideal.log.controller.model.ControllerLog;
import cn.sh.ideal.log.controller.service.ControllerLogService;

/**
 * ClassName:ControllerLogServiceImpl <br/>
 * Function: 接口日志service Date: 2015年2月5日 下午4:38:38 <br/>
 * 
 * @author GengHongChuang
 * @version
 * @since JDK 1.6
 * @see
 */
@Service("controllerLogService")
public class ControllerLogServiceImpl implements ControllerLogService {
    private static final Logger logger = LoggerFactory
            .getLogger(ControllerLogServiceImpl.class);
    @Autowired
    private LogControllerSqlSession logControllerSqlSession;

    public SqlSession getSqlSession() {
        return logControllerSqlSession.getSessionFactory().openSession();
    }

    @Override
    public void log(ControllerLog log) throws Exception {
       
        SqlSession sqlSession = getSqlSession();
        try {
            sqlSession
                    .insert("cn.sh.ideal.log.controller.db.mapping.ControllerLogMapper.insertLog",
                            log);
            sqlSession.commit();
        } catch (Exception e) {

            logger.error("插入日志信息异常", e.getMessage());
			
             //throw e;
        } finally {
            sqlSession.close();
            
        }
        

    }

    @Override
    public void clear() {
        SqlSession sqlSession = getSqlSession();

        try {

            sqlSession
                    .delete("cn.sh.ideal.log.controller.db.mapping.ControllerLogMapper.clearLog",
                            null);
            sqlSession.commit();
            // this.remove("ControllerLogMapper.clearLog", null);
        } catch (Exception e) {
            logger.error("定时清除接口调用日志异常", e.getMessage());
        } finally {
            sqlSession.close();
        }

    }

    public LogControllerSqlSession getLogControllerSqlSession() {
        return logControllerSqlSession;
    }

    public void setLogControllerSqlSession(
            LogControllerSqlSession logControllerSqlSession) {
        this.logControllerSqlSession = logControllerSqlSession;
    }

}
